// Created from GroupGenerationTemplate
// For Decisions HL7 Engine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using DecisionsFramework.Design.Properties;
using Decisions.HL7.DataTypes;
using ST = System.String;
using SI = System.String;
using IS = System.String;
using ID = System.String;
using SNM = System.String;

namespace Decisions.HL7.Message
{
    [DataContract]
	public class ORDER_PRIOR_OPL_O37 : AbstractGroup
    {
        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("55 OBR",55)]
  	    [HL7Field(HL7Version.v26, 55, HL7Repeat.False, HL7Required.True,"OBR")]
	    public OBR OBR {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("56 ORC",56)]
  	    [HL7Field(HL7Version.v26, 56, HL7Repeat.False, HL7Required.False,"ORC")]
	    public ORC ORC {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("57 ROL",57)]
  	    [HL7Field(HL7Version.v26, 57, HL7Repeat.True, HL7Required.False,"ROL")]
	    public ROL[] ROL {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("58 TIMING",58)]
  	    [HL7Field(HL7Version.v26, 58, HL7Repeat.False, HL7Required.False,"TIMING_OPL_O37")]
	    public TIMING_OPL_O37 TIMING {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("62 OBX",62)]
  	    [HL7Field(HL7Version.v26, 62, HL7Repeat.True, HL7Required.True,"OBX")]
	    public OBX[] OBX {get;set;}



		#region Parser Helpers and Conrete Impls

        internal override void ConsumeTextSegmentsForGroup(Stack<string> availableData, HL7ParserOptions options) {

			OBR = PopSegmentParseAndCheckRequired<OBR>(availableData, options, "OBR");
			ORC = PopSegmentParseAndCheckRequired<ORC>(availableData, options, "ORC");
			ROL = PopSegmentParseAndCheckRequiredArray<ROL>(availableData, options, "ROL");
			TIMING = ProcessGroup<TIMING_OPL_O37>(availableData, options, "TIMING");
			OBX = PopSegmentParseAndCheckRequiredArray<OBX>(availableData, options, "OBX");
        }

		internal override bool CanConsume(string availableData, HL7ParserOptions options)
        {
			bool isOBR = availableData.StartsWith("OBR", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("OBR", options.Version) || isOBR) {
				return isOBR;
			}
			bool isORC = availableData.StartsWith("ORC", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("ORC", options.Version) || isORC) {
				return isORC;
			}
			bool isROL = availableData.StartsWith("ROL", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("ROL", options.Version) || isROL) {
				return isROL;
			}
			TIMING_OPL_O37 testTIMING_OPL_O37 = new TIMING_OPL_O37();
			bool grpCCTIMING_OPL_O37 = (testTIMING_OPL_O37.CanConsume(availableData, options));
			if (IsRequired("TIMING", options.Version) || grpCCTIMING_OPL_O37) {
				return grpCCTIMING_OPL_O37;
			}
			bool isOBX = availableData.StartsWith("OBX", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("OBX", options.Version) || isOBX) {
				return isOBX;
			}
			return false;
        }

        private static Dictionary<string, bool> SEGMENT_VERSION_REQUIRED_MAP = new Dictionary<string, bool>();

		protected override Dictionary<string, bool> MAP_OF_SEGMENT_TO_REQUIRED
        {
            get
			{
				return SEGMENT_VERSION_REQUIRED_MAP;
			}
        }

		public override string HL7ENTITY_TYPE_NAME
        {
            get {
				return "ORDER_PRIOR";
			}
        }

        static ORDER_PRIOR_OPL_O37() {

             SetupStaticPropertyMap(SEGMENT_VERSION_REQUIRED_MAP, typeof(ORDER_PRIOR_OPL_O37));

        }

		#endregion

		public override string GetER7(HL7ParserOptions options)
		{
			List<string> segmentData = new List<string>();
		 if (OBR != null) {
			string segmentValue = OBR.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (ORC != null) {
			string segmentValue = ORC.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (ROL != null && ROL.Length > 0) {
			for (int i = 0; i < ROL.Length; i++) {
				string segmentValue = ROL[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
		 if (TIMING != null) {
			string segmentValue = TIMING.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (OBX != null && OBX.Length > 0) {
			for (int i = 0; i < OBX.Length; i++) {
				string segmentValue = OBX[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
           return string.Join(HL7ParserConstants.SEGMENT_SEPARATOR, segmentData.ToArray());
        
        }
    }
}

